package com.yc.tank;

import java.awt.Graphics;
import java.awt.Rectangle;

import com.yc.tank.util.PropertiesTools;

public class Bullet {

	private TankFrame tf;
	//子弹速度
	private int speed = PropertiesTools.getInt("bulletSpeed");
	//子弹位置
	private int x;
	private int y;
	//子弹行进方向
	private Dir dir;
	//子弹的实质内核（用于碰撞检测）
	private Rectangle rect;
	//子弹是否还存活(还在画面中)
	private boolean live = true;
	//子弹是否还存活（还没炸毁）
	private boolean living = true;
	//子弹分组
	private Group group;
	
	public Bullet(int x, int y, Dir dir, Group group, TankFrame tf) {
		super();
		this.x = x;
		this.y = y;
		this.dir = dir;
		this.tf = tf;
		this.group = group;
		rect = new Rectangle(x, y, ResourceMgr.bulletMax, ResourceMgr.bulletMin);
	}
	
	public Rectangle getRect() {
		return rect;
	}

	public Group getGroup() {
		return group;
	}

	/**
	 * 绘制子弹
	 * @param g
	 */
	public void paint(Graphics g) {
		if (!live || !living)
			tf.bullets.remove(this);
		
		switch(dir) {
		case LIFT:
			g.drawImage(ResourceMgr.bulletL, x, y, null);
			break;
		case UP:
			g.drawImage(ResourceMgr.bulletU, x, y, null);
			break;
		case RIGHT:
			g.drawImage(ResourceMgr.bulletR, x, y, null);
			break;
		case DOWN:
			g.drawImage(ResourceMgr.bulletD, x, y, null);
			break;
		default:
			break;
					
		}
		
		move();
	}
	
	
	private void move() {
		//这里如果用if就显得比较麻烦，每次还需要写“方向==xxx”
		switch(dir) {
		case LIFT://这里的写法，不要写成Dir.LIFT
			x -= speed;
			break;
		case UP:
			y -= speed;
			break;
		case RIGHT:
			x += speed;
			break;
		case DOWN:
			y += speed;
			break;
		}
		rect.setLocation(x, y);//改变子弹灵魂位置
		if (x < 0 || y < 0 || x > tf.GAME_WIDTH || y > tf.GAME_HEIGTH) live = false;//越界死亡
	}
	
	/**
	 * 销毁
	 */
	public void die() {
		living = false;
		
	}
}
